<?php
class Vfriend_Common extends Vfriend_Db {
	var $cache;
	var $clear;
	public function __construct() {
		$this->cache = new Vfriend_Cache();
		$this->clear = $this->cache->clear;
	}
	public function getCountry($isCombobox=1) {
		try {
			$strKey 	= COMMON_GETCOUNTRY;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call com_getcountries();');
				$arrData = $this->fetchAll();
				$this->cache->set($strKey, $arrData, COMMON_GETCOUNTRY_EXPIRE);
			}
			if($isCombobox) {
				$arrResult = array();
				foreach($arrData as $value) {
					$arrResult[$value['code']] = $value['name'];
				}
				return $arrResult;
			}
			else
				return isset($arrData[0])?$arrData[0]:null;
		}catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_Common::getCountry - ' .$e->getMessage());			
			return null;
		}		
	}
	public function getProvince($strCountry='VN',$isCombobox=1) {
		try {
			$strKey		= COMMON_GETPROVINCE . $strCountry;			
			$arrData	= $this->cache->get($strKey);	
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call com_getprovinces(?);');
				$this->bindParam(1, $strCountry, PDO::PARAM_STR, 2);
				$arrData = $this->fetchAll();
				$this->cache->set($strKey, $arrData, COMMON_GETPROVINCE_EXPIRE);				
			}
			if($isCombobox) {
				$arrResult = array();
				foreach($arrData as $value) {
					$arrResult[$value['provinceid']] = $value['name'];
				}
				return $arrResult;
			}else {
				return isset($arrData[0])?$arrData[0]:null;
			}
		}catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_Common::getProvince - ' .$e->getMessage());			
			return null;
		}		
	}
	public function getDistrict($intProvinceId,$isCombobox=1) {
		try {
			$strKey 	= COMMON_GETDISTRICT . $intProvinceId;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call com_getdistricts(?);');
				$this->bindParam(1, $intProvinceId, PDO::PARAM_INT,11);
				$arrData = $this->fetchAll();
				$this->cache->set($strKey, $arrData, COMMON_GETDISTRICT_EXPIRE);
			}
			if($isCombobox) {
				$arrResult = array();
				foreach($arrData as $value) {
					$arrResult[$value['districtid']] = $value['name'];
				}
				return $arrResult;
			}else {
				return isset($arrData[0])?$arrData[0]:null;
			}
		}catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_Common::getDistrict - ' .$e->getMessage());			
			return null;
		}		
	}
	public function getEducation($intUserId=0, $isCombobox=1) {
		try {
			$strKey 	= COMMON_GETEDUCATION . $intUserId;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call com_education(?);');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrData = $this->fetchAll();
				$this->cache->set($strKey, $arrData, COMMON_GETEDUCATION_EXPIRE);
			}
			if($isCombobox) {
				$arrResult = array();
				foreach($arrData as $strRow) {
					$arrResult[$strRow['id']] = $strRow['name'];
				}
				return $arrResult;
			}
			else {
				return isset($arrData)?$arrData:null;
			}
			
		}catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_Common::getEducation - ' .$e->getMessage());			
			return null;
		}		
	}
	public function getOccupation($intUserId=0, $isCombobox=1) {
		try {
			$strKey 	= COMMON_GETOCCUPATION . $intUserId;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call com_occupation(?);');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrData = $this->fetchAll();
				$this->cache->set($strKey, $arrData, COMMON_GETOCCUPATION_EXPIRE);
			}
			if($isCombobox) {
				$arrResult = array();
				foreach($arrData as $strRow) {
					$arrResult[$strRow['id']] = $strRow['name'];
				}
				return $arrResult;
			}
			else {
				return isset($arrData)?$arrData:null;
			}
		}catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_Common::getOccupation - ' .$e->getMessage());			
			return null;
		}		
	}
	public function getPosition($intUserId=0, $isCombobox=1) {
		try {
			$strKey 	= COMMON_GETPOSITION . $intUserId;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call com_position(?);');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrData = $this->fetchAll();
				$this->cache->set($strKey, $arrData, COMMON_GETPOSITION_EXPIRE);
			}
			if($isCombobox) {
				$arrResult = array();
				foreach($arrData as $strRow) {
					$arrResult[$strRow['id']] = $strRow['name'];
				}
				return $arrResult;
			}
			else {
				return isset($arrData)?$arrData:null;
			}
		}catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_Common::getPosition - ' .$e->getMessage());			
			return null;
		}		
	}
	public function getMarital($intUserId=0, $isCombobox=1) {
		try {
			$strKey 	= COMMON_GETMARITAL . $intUserId;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call com_marital(?);');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrData = $this->fetchAll();
				$this->cache->set($strKey, $arrData, COMMON_GETMARITAL_EXPIRE);
			}
			if($isCombobox) {
				$arrResult = array();
				foreach($arrData as $strRow) {
					$arrResult[$strRow['id']] = $strRow['name'];
				}
				return $arrResult;
			}
			else {
				return isset($arrData)?$arrData:null;
			}
		}catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_Common::getMarital - ' .$e->getMessage());			
			return null;
		}		
	}
	public function getReligion($intUserId=0, $isCombobox=1) {
		try {
			$strKey 	= COMMON_GETRELIGION . $intUserId;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call com_religion(?);');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$arrData = $this->fetchAll();
				$this->cache->set($strKey, $arrData, COMMON_GETRELIGION_EXPIRE);
			}
			if($isCombobox) {
				$arrResult = array();
				foreach($arrData as $strRow) {
					$arrResult[$strRow['id']] = $strRow['name'];
				}
				return $arrResult;
			}
			else {
				return isset($arrData)?$arrData:null;
			}
		}catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_Common::getReligion - ' .$e->getMessage());			
			return null;
		}		
	}
	public function getPreference($intQuestionId=0, $isCombobox=0) {
		try {
			$strKey 	= COMMON_GETPREFERENCE . $intQuestionId;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call com_preference_answer(?);');
				$this->bindParam(1, $intQuestionId, PDO::PARAM_INT, 11);
				$arrData = $this->fetchAll();
				$this->cache->set($strKey, $arrData, COMMON_GETPREFERENCE_EXPIRE);
			}
			if($isCombobox) {
				$arrResult = array();
				foreach($arrData as $strRow) {
					$arrResult[$strRow['id']] = $strRow['name'];
				}
				return $arrResult;
			}
			else {
				return isset($arrData)?$arrData:null;
			}
		}catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_Common::getPreference - ' .$e->getMessage());			
			return null;
		}		
	}
	public function addCategory($intUserId, $intType, $strName, $intParentId=0, $intDisorder=0) {
		try {
			$this->prepare('call categories_add(?,?,?,?,?);');
			$this->bindParam(1, $intUserId	, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intType	, PDO::PARAM_INT, 4);
			$this->bindParam(3, $strName	, PDO::PARAM_STR, 150);
			$this->bindParam(4, $intParentId, PDO::PARAM_INT, 11);
			$this->bindParam(5, $intDisorder, PDO::PARAM_INT, 11);
			$arrResult 		= $this->fetchAll();
			$intCategoryId	= isset($arrResult[0]['categoryid']) ? $arrResult[0]['categoryid'] : 0;
			if($intCategoryId > 0) {
				$this->clear->deleteCommonByUserId($intUserId);
			}
			return $intCategoryId;
			
		}catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_Common::addCategory - ' .$e->getMessage());			
			return null;
		}	
	}
	public function getCategory($intParentId=0, $intType=0, $intTproduct=0, $intCombox=1) {
		try {	
			$arrData	= null;
			$strKey 	= COMMON_GETCATEGORY . $intParentId . $intType . $intTproduct;
			$arrData	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call categories_getall_com(?,?,?)');
				$this->bindParam(1, $intParentId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intType, PDO::PARAM_INT, 4);
				$this->bindParam(3, $intTproduct, PDO::PARAM_INT, 4);
				$arrData 	= $this->fetchAll();
				$this->cache->set($strKey, $arrData, COMMON_GETCATEGORY_EXPIRE);
			}			
			if($intCombox) {
				$arrResult	= null;
				if($arrData) {
					foreach($arrData as $value) {
						$arrResult[$value['categoryid']] = $value['name'];
					}
				}				
				return $arrResult;
			}			
			return $arrData;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_Common::getCategory - ' .$e->getMessage());			
			return null;
		}
	}
	public function getCategoryById($intCategoryId) {
		try {	
			$strKey 	= COMMON_GETCATEGORYBYID . $intCategoryId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call categories_get(?)');
				$this->bindParam(1, $intCategoryId, PDO::PARAM_INT, 11);
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrData, COMMON_GETCATEGORYBYID_EXPIRE);
			}			
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_Common::getCategoryById - ' .$e->getMessage());			
			return null;
		}
	}
	public function getCategoryByType($intType) {
		try {	
			$strKey 	= COMMON_GETCATEGORYBYTYPE . $intType;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrData)) {
				$this->prepare('call categories_get_type(?)');
				$this->bindParam(1, $intType, PDO::PARAM_INT, 4);
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrData, COMMON_GETCATEGORYBYTYPE_EXPIRE);
			}			
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_Common::getCategoryByType - ' .$e->getMessage());			
			return null;
		}
	}
	public function getSeo($strController, $strAction) {
		try {
			$strKey 	= COMMON_GETSEO . $strController . $strAction;
			$arrResult 	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call seos_get_ca(?,?)');
				$this->bindParam(1, $strController, PDO::PARAM_STR, 50);
				$this->bindParam(2, $strAction, PDO::PARAM_STR, 50);
				$arrResult	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, COMMON_GETSEO_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		}catch(Exception $e) {
			Vfriend_Function::logDb('Vfriend_Common::getSeo - ' .$e->getMessage());			
			return null;
		}
	}
}
?>
